package com.hyacinth.util.model;

import org.springframework.data.domain.Page;

import java.util.List;

/**
 * 将查询得到的分页结果或者条件查询结果集转换为PageBean对象
 */
public class PageBeanFactory<T> {
    /**
     * 单例创建PageBeanFactory
     */
    private PageBeanFactory(){}
    private static class PageBeanSingle{
        private static final PageBeanFactory INSTANCE = new PageBeanFactory();
    }
    public static PageBeanFactory getInstance() {
        return PageBeanSingle.INSTANCE;
    }

    /**
     * 将Page对象转换为PageBean对象
     */
    public PageBean<T> getPageBeanByPage(Page<T> page, List<T> list){
        PageBean<T> pageBean = new PageBean<T>();
        pageBean.setPageNum(page.getNumber());
        pageBean.setPageSize(page.getSize());
        pageBean.setTotalSize(page.getTotalElements());
        pageBean.setTotalPages(page.getTotalPages());
        pageBean.setRows(list);
        return pageBean;
    }

    /**
     * 将Page对象转换为PageBean对象
     */
    public PageBean<T> getPageBeanByPage(Page<T> page){
        PageBean<T> pageBean = new PageBean<T>();
        pageBean.setPageNum(page.getNumber());
        pageBean.setPageSize(page.getSize());
        pageBean.setTotalSize(page.getTotalElements());
        pageBean.setTotalPages(page.getTotalPages());
        pageBean.setRows(page.getContent());
        return pageBean;
    }

    /**
     * 将Page对象转换为PageBean对象
     */
    public PageBean<T> getPageBeanByList(List<T> rows){
        PageBean<T> pageBean = new PageBean<T>();
        pageBean.setPageNum(-1);
        pageBean.setPageSize(0);
        pageBean.setTotalSize(rows.size());
        pageBean.setTotalPages(0);
        pageBean.setRows(rows);
        return pageBean;
    }

    /*------------------------jquery datatables 分页组装类------------------------/

    /**
     * 将Page对象转换为ResponsePaging对象
     * @param page 分页参数
     * @param rows 分页后数据
     * @return ResponsePaging
     */
    public DataTabPageBean<T> getRespPagingByList(Page<T> page, List<T> rows) {
        DataTabPageBean<T> responsePaging = new DataTabPageBean<>();
        responsePaging.setData(rows);
        responsePaging.setRecordsTotal(page.getTotalElements());
        responsePaging.setRecordsFiltered(page.getTotalElements());
        return responsePaging;
    }
    /**
     * 将Page对象转换为ResponsePaging对象
     *
     * @param page 分页参数
     * @return ResponsePaging
     */
    public DataTabPageBean<T> getRespPagingByList(Page<T> page) {
        DataTabPageBean<T> responsePaging = new DataTabPageBean<>();
        responsePaging.setData(page.getContent());
        responsePaging.setRecordsTotal(page.getTotalElements());
        responsePaging.setRecordsFiltered(page.getTotalElements());
        return responsePaging;
    }
    /**
     * 将Page对象转换为ResponsePaging对象
     *
     * @param rows 分页后数据
     * @return ResponsePaging
     */
    public DataTabPageBean<T> getRespPagingByList(List<T> rows) {
        DataTabPageBean<T> responsePaging = new DataTabPageBean<>();
        responsePaging.setData(rows);
        responsePaging.setRecordsTotal(rows.size());
        responsePaging.setRecordsFiltered(rows.size());
        return responsePaging;
    }
}
